package com.tansun.easycare.rule.common.rulebasebean;

import java.util.ArrayList;
import java.util.List;

import com.tansun.easycare.core.utils.StringUtils;


public class DWhen {
    /**
     * 必须部分Fact 用于提供数据对象的类型为IN OUT All的Map
     */
    private Fact firstFact = new Fact();
    /**
     * 变量引用，用于引用对象模型的属性
     */
    private List<Fact> factList = new ArrayList<Fact>();
    /**
     * 表达式 boolean
     */
    private List<Fact> evalList = new ArrayList<Fact>();
    private String drlObj = "";
    /**
     * 返回 evallist 和factlist的处理逻辑相同 是在两map()中书写drl代码 BlanckFact 没有Map()包括 Fact 一个fact一个Map()
     * groupFact 按照顺序生成，多个groupFact在一个Map()
     */
    public String getRuleContent() {
        StringBuffer strBuffer = new StringBuffer();

        strBuffer.append("\t$insertMap : Map(\n");
        strBuffer.append("\t\t" + getFirstFact().getContent() + "\n");
        strBuffer.append("\t)\n");

        if (factList.size() > 0) {
            //Collections.sort(factList);
        	List<Fact> entryFacts = new ArrayList<Fact>();
            List<Fact> ruleFacts = new ArrayList<Fact>();
            for (int i = 0; i < factList.size(); i++) {
            	Fact eval = factList.get(i);
            	if( Fact.TYPE_ENTRY == eval.getType() ) {
            		entryFacts.add(eval);
            	}else {
            		ruleFacts.add(eval);
            	}
            }
            String entryCond = fact2Str(entryFacts);
            String ruleCond = fact2Str(ruleFacts);
            strBuffer.append("\t\t" + this.drlObj + "\n");
            strBuffer.append("\tMap(\n");
            if(StringUtils.isNotEmpty(entryCond) && StringUtils.isNotEmpty(ruleCond)) {
            	strBuffer.append("\t\t(\n");
            	strBuffer.append("\t"+entryCond);
            	strBuffer.append("\t\t)&&(\n");
            	strBuffer.append("\t"+ruleCond);
            	strBuffer.append("\t\t)\n");
            }else if(StringUtils.isNotEmpty(entryCond) && StringUtils.isEmpty(ruleCond) ) {
            	strBuffer.append(entryCond);
            }else if(StringUtils.isEmpty(entryCond) && StringUtils.isNotEmpty(ruleCond) ) {
            	strBuffer.append(ruleCond);
            }
            strBuffer.append("\t)\n");
        }
        if (evalList.size() > 0) {
            List<Fact> entryFacts = new ArrayList<Fact>();
            List<Fact> ruleFacts = new ArrayList<Fact>();
            for (int i = 0; i < evalList.size(); i++) {
            	Fact eval = evalList.get(i);
            	if( Fact.TYPE_ENTRY == eval.getType() ) {
            		entryFacts.add(eval);
            	}else {
            		ruleFacts.add(eval);
            	}
            }
            String entryCond = fact2Str(entryFacts);
            String ruleCond = fact2Str(ruleFacts);
            strBuffer.append("\t\t" + this.drlObj + "\n");
            strBuffer.append("\tMap(\n");
            if(StringUtils.isNotEmpty(entryCond) && StringUtils.isNotEmpty(ruleCond)) {
            	strBuffer.append("\t\t(\n");
            	strBuffer.append("\t"+entryCond);
            	strBuffer.append("\t\t)&&(\n");
            	strBuffer.append("\t"+ruleCond);
            	strBuffer.append("\t\t)\n");
            }else if(StringUtils.isNotEmpty(entryCond) && StringUtils.isEmpty(ruleCond) ) {
            	strBuffer.append(entryCond);
            }else if(StringUtils.isEmpty(entryCond) && StringUtils.isNotEmpty(ruleCond) ) {
            	strBuffer.append(ruleCond);
            }
            strBuffer.append("\t)\n");
        }
        return strBuffer.toString();
    }

    public String fact2Str(List<Fact> facts) {
    	 StringBuffer strBuffer = new StringBuffer();
    	 for (int i = 0; i < facts.size(); i++) {
    		 Fact eval = facts.get(i);
    		 String evalStr = eval.getContent();
    		 if(StringUtils.isEmpty(evalStr)) {
    			 return strBuffer.toString();
    		 }
    		 if(evalStr.endsWith("\n")) {
    			 evalStr = evalStr.substring(0, evalStr.length()-2);
    		 }
    		 if (eval instanceof GroupFact) {
                 if ((i + 1) == facts.size() || !(facts.get(i + 1) instanceof GroupFact)) {//最后一个
                 	if(StringUtils.isEmpty(eval.getLineSuffix()) && StringUtils.isEmpty(eval.getLinePrefix())) {//没有逻辑符号
                     	strBuffer.append("\t\t(" + eval.getLinePrefix() + evalStr + eval.getLastLineSuffix()+")\n");
                 	}else{
                     	strBuffer.append("\t\t" + eval.getLinePrefix() + evalStr + eval.getLastLineSuffix()+"\n");
                 	}
                 } else {
                 	if(StringUtils.isEmpty(eval.getLineSuffix()) && StringUtils.isEmpty(eval.getLinePrefix())) {
                 		strBuffer.append("\t\t(" + eval.getContent() + ") &&\n");
                 	}else{
                 		strBuffer.append("\t\t" + eval.getLinePrefix() + evalStr + eval.getLineSuffix()+"\n");
                 	}
                 }
             } else {
            	 if (eval instanceof BlankFact) {
            		 BlankFact blank = (BlankFact) eval;
            		 if("score".equals(blank.getFactType())) {
            			 this.drlObj = "\t\t" + factList.get(i).getContent() + "\n";
            		 }else {
            			 strBuffer.append("\t\t" + eval.getLinePrefix() + evalStr + eval.getLineSuffix()+"\n");
            		 }
                 }else {
                	 strBuffer.append("\t\t" + eval.getLinePrefix() + evalStr + eval.getLineSuffix()+"\n");
                 }
                
             }
         }
		return strBuffer.toString();
    }
    public Fact getFirstFact() {
        return firstFact;
    }

    public void setFirstFact(Fact firstFact) {
        this.firstFact = firstFact;
    }

    public List<Fact> getFactList() {
        return factList;
    }

    public void setFactList(List<Fact> factList) {
        this.factList = factList;
    }

    //	public List<Eval> getEvalList() {
//		return evalList;
//	}
//	public void setEvalList(List<Eval> evalList) {
//		this.evalList = evalList;
//	}
    public List<Fact> getEvalList() {
        return evalList;
    }

    public void setEvalList(List<Fact> evalList) {
        this.evalList = evalList;
    }

}
